﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class LevelBll:ILevelBll
    {
        private ILevelDal _dal;

        public LevelBll(ILevelDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddLevelAsync(string Title, string Details)
        {
            return await _dal.AddModelAsync(new Level
            {
                title = Title,
                details = Details
            });
        }

        public async Task<int> DeleteLevelAsync(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _dal.RemoveModelAsync(data);
        }

        public async Task<int> EditLevelAsync(int Id, string Title, string Details)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.title = Title;
            data.details = Details;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditModelAsync(data);
        }

        public async Task<IEnumerable<LevelDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new LevelDtos
            {
                id = r.Id,
                title = r.title,
                details = r.details,
              
            }) : new List<LevelDtos>();
        }

        public async Task<LevelDtos> GetDataById(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            return data == null ? new LevelDtos() : new LevelDtos
            {
                id = data.Id,
                title = data.title,
                details = data.details,
               
            };
        }

        public async Task<IEnumerable<LevelDtos>> GetDataByTitle(string Title, int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId && r.title.Contains(Title));
            return data == null ? new List<LevelDtos>() : data.Select(r => new LevelDtos
            {
                id = r.Id,
                title = r.Title,
                details = r.Details,
           
            });
        }
    }
}
